package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 刘贤斌 菜单显示
 */

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;
    //分页查询
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();//ThreadLocal
        PageHelper.startPage(currentPage,pageSize);//分页插件，会在执行sql之前将分页关键字追加到SQL后面
        Page<Menu> page = menuDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }
   //根据id获取子Menu
    @Override
    public List<Menu> getSonMenuByID(Integer id) {
        List<Menu>menuList=menuDao.getSonMenuByID(id);
        return menuList;
    }
    @Override
    public List<Menu> getAllMenu() {
        List<Menu>menuList=menuDao.getAllMenu();
        return menuList;
    }
    @Override
    public void add(Menu menu) {
        menuDao.add(menu);
    }

    @Override
    public void edit(Menu menu) {
        menuDao.edit(menu);
    }

    @Override
    public void delete(Integer id) {
        //1 查看是否是其他菜单的父菜单
        List<Menu> menuList= menuDao.isFatherMenu(id);
        //2 查看是否被角色引用即t_role_menu中是否有数据
        List<Menu> roleMenuList=menuDao.findRoleMenuById(id);
        if(menuList.size()>0 ){
            throw new RuntimeException("该菜单存在子菜单，不允许删除");
        }else if(roleMenuList.size()>0){
            throw new RuntimeException("该菜单被角色使用，不允许删除");
        }else if(menuList.size()>0&&roleMenuList.size()>0){
            throw new RuntimeException("该菜单存在子菜单且被角色使用，不允许删除");
        } else {
            menuDao.deleteById(id);
        }

    }
    @Override
    public List<Menu> findMenuByName(String name) {
        //通过递归 查询不确定层级的菜单
        //1 首先查出第一层级的菜单
        List<Menu>  menuList= menuDao.getLevelOneMenuByUserName(name);
        //2 递归查询
        return   getMenuById(menuList);
    }
    public List<Menu> getMenuById(List<Menu> menuList){
        for (int i = 0; i < menuList.size(); i++) {
            Integer id = menuList.get(i).getId();
            List<Menu> menuListSon = menuDao.getMenuById(id);
            if(menuListSon!=null&&menuListSon.size()>0){
                menuList.get(i).setChildren(menuListSon);
                getMenuById(menuListSon);
            } else{
                System.out.println(menuList);
                return menuList;
            }
        }
        System.out.println(menuList);
        return  menuList;
    }

}